package com.zqweb.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zqweb.gulimall.ware.vo.MergeVo;
import com.zqweb.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.ware.entity.PurchaseEntity;
import com.zqweb.gulimall.ware.service.PurchaseService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 采购信息
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 20:28:38
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;


    /**
     * 将选中的采购需求合并到某个采购单中，如果没有采购单则先新建，再获取新建后的采购单id，最后将
     * 采购需求添加到这个新建的采购单中，否则就直接合并到已有的控件采购单即可！
     */
    @PostMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        purchaseService.merge(mergeVo);
        return R.ok();
    }

    /**
     * 传入要接收的采购单id数组，用于采购人员进行接收、领取
     */
    @PostMapping("/receive")
    public R receivePurchase(@RequestBody List<Long> ids){
        purchaseService.receivePurchase(ids);
        return R.ok();
    }

    /**
     * 采购完成处理
     */
    @PostMapping("/done")
    public R done(@RequestBody PurchaseDoneVo doneVo){
        purchaseService.done(doneVo);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询空闲的没有被领取的采购单
     */
    @RequestMapping("/unreceive/list")
    public R getUnReceivePurchaseForms(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.getUnReceivePurchaseForms(params);

        return R.ok().put("page",page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
